//
//  LWStatusRetweetView.swift
//  LW_Weibo
//
//  Created by 刘伟 on 16/7/13.
//  Copyright © 2016年 LWSpro. All rights reserved.
//

import UIKit
import SnapKit

class LWStatusRetweetView: UIView {

    //记录冲突约束,这个约束就是当前view的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    
    //添加属性  为了给内容赋值
    var statusViewModel: LWStatusViewModel? {
        
        //set方法 重写
        didSet {
            
            
            //避免冲突  cell重用时,pictureCollectionView 的视图约束可能是存在的  而当没有图片的时候,这时当前view的底部约束就需要改变了  这样就会冲突了
            retweetViewBottomConstraint?.uninstall()
            
            
            //在这里面给子控件赋值
            contentLabel.text = statusViewModel?.retweetContent
            
            
            
            //判断是否有图片存在
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {

                
                
                //显示图片
                pictureCollectionView.hidden = false
                
                //赋值
                pictureCollectionView.pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls
                
                //重新布局  即更改
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    
                    
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureCollectionView.snp_bottom).offset(HomeCellMargin).constraint
                    
                })
                
                
            }else {
                
                //隐藏
                pictureCollectionView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    
                    
                   self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                    
                })
                
                
            }
            
            
           
            
            
        }
  
    }
    
    
    
    
    //MARK: - 懒加载
    private lazy var contentLabel: UILabel = {
    
    
        
        let label = UILabel(textColor: UIColor.grayColor(), titleFont: 14)
       
        
        //  多行显示
        label.numberOfLines = 0
        
        
        return label
    
    }()
    

    
    //  转发微博配图
    private lazy var pictureCollectionView: LWStatusPictureCollectionView = {
        
        let collectionView = LWStatusPictureCollectionView()
        //  设置配图的背景色跟转发微博的背景色一致
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
        
    }()

    
    
    
    //MARK: - 重写
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        layoutUI()
        
        
    }

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - 约束
    private func layoutUI() {
        
        addSubview(contentLabel)
        addSubview(pictureCollectionView)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            
            make.trailing.equalTo(self).offset(-HomeCellMargin)
            
        }
        
        
        pictureCollectionView.snp_makeConstraints { (make) -> Void in
            
            
             make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            
           
            
            
        }
        
        
        
        
        self.snp_makeConstraints { (make) -> Void in
            
            
            self.retweetViewBottomConstraint = make.bottom.equalTo(pictureCollectionView.snp_bottom).offset(HomeCellMargin).constraint
            
        }
        
        
    }
    
    
    
        
    
}
